package com.fiveflavors.suittar.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import twitter4j.UserWithStatus;

import com.fiveflavors.suittar.Suittar;
import com.fiveflavors.suittar.gui.inputpanel.SuittarInputPanel;
import com.fiveflavors.suittar.gui.mainpanel.SuittarMainPane;
import com.fiveflavors.suittar.gui.statusbar.SuittarStatusBar;

/**
 * @author masaaki
 *
 */
public class SuittarFrame extends JFrame {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5873188407996575275L;
	public static final int TAB_MAIN_PUBLIC = 0;
	public static final int TAB_MAIN_MYUSER = 1;
	public static final int TAB_MAIN_ABOUT = Integer.MAX_VALUE;
	
    private JTabbedPane tabPane = null;

    private ArrayList<Integer> userTabArray = new ArrayList<Integer>();
    
    private SuittarMainPane publicMainPane = null;
    private SuittarTabbedPane ownerTabbedPane = null;
    private SuittarMainPane aboutMainPane = null;
    private SuittarInputPanel inputPane = null;
    private SuittarStatusBar statusBar = null;
    
    private SuittarTabChangeListener suittarTabChangeListener = null;
    
    private Font font = null;
    
    public SuittarFrame(Suittar suittar) {
		super("Suittar!");
		initializeGui();
		suittarTabChangeListener = new SuittarTabChangeListener(suittar,false);
		setSelectedTabIndex(TAB_MAIN_ABOUT,TAB_MAIN_ABOUT);
	}
	
	private void initializeGui() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    
        getContentPane().setLayout(new BorderLayout());

	    GridBagLayout layout = new GridBagLayout();

	    JPanel p = new JPanel();
        p.setLayout(layout);
        
        GridBagConstraints gbc = new GridBagConstraints();
        
        //add rightPane
        JPanel mainPane = createMainPane();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 100.0;
        gbc.weighty = 100.0;
        gbc.fill = GridBagConstraints.BOTH;
        layout.setConstraints(mainPane, gbc);
        p.add(mainPane);

        //add InputPanel
        inputPane = new SuittarInputPanel();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 100.0;
        gbc.weighty = 0;
        layout.setConstraints(inputPane, gbc);
        p.add(inputPane);
        
        //add StatusBar
        statusBar = new SuittarStatusBar();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridheight = GridBagConstraints.REMAINDER;
        layout.setConstraints(statusBar, gbc);
        p.add(statusBar);
        
        getContentPane().add(p,BorderLayout.CENTER);
        pack();
	    setSize(550,600);

        
	    setLocationRelativeTo(null);

	}
	
	private JPanel createMainPane() {
		
	    JPanel p = new JPanel();
	    GridBagLayout layout = new GridBagLayout();
        p.setLayout(layout);        
        GridBagConstraints gbc = new GridBagConstraints();
                
        tabPane = new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
        Font font = new Font(tabPane.getFont().getFontName(),tabPane.getFont().getStyle(),11);
        tabPane.setFont(font);

        publicMainPane = new SuittarMainPane();
        tabPane.addTab("Public", publicMainPane);
        
        ownerTabbedPane = new SuittarTabbedPane();
        tabPane.addTab("Home", ownerTabbedPane);
        
        aboutMainPane = new SuittarMainPane();
        tabPane.addTab("About", aboutMainPane);
        
        aboutMainPane.showAbout();

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 100.0;
        gbc.weighty = 100.0;
        gbc.fill = GridBagConstraints.BOTH;
        layout.setConstraints(tabPane, gbc);
        p.add(tabPane);

        return p;
	}
	
	public SuittarInputPanel getInputPane() {
		return inputPane;
	}
	
	public SuittarStatusBar getStatusBar() {
		return statusBar;
	}
	
	public void setMainPaneFont(Font font) {
		this.font = font;
		publicMainPane.setMainPaneFont(font);
		aboutMainPane.setMainPaneFont(font);
		publicMainPane.showAbout();
		for (SuittarMainPane pane:ownerTabbedPane.getPaneAll()) {
			pane.setMainPaneFont(font);
		}
	}
	
	public void setSelectedTabIndex(int index,int subIndex) {
		
		if (index == TAB_MAIN_PUBLIC) {
			tabPane.setSelectedIndex(TAB_MAIN_PUBLIC);			
		}
		else if (index == TAB_MAIN_ABOUT) {
			tabPane.setSelectedIndex(tabPane.getTabCount() - 1);			
		}
		else if (index == TAB_MAIN_MYUSER) {
			tabPane.setSelectedIndex(TAB_MAIN_MYUSER);
			ownerTabbedPane.setSelectedIndex(subIndex);
		}
		else {
			tabPane.setSelectedIndex(index);
			SuittarTabbedPane userTabbedPane = (SuittarTabbedPane)tabPane.getComponentAt(index);
			userTabbedPane.setSelectedIndex(subIndex);
		}
	}

	public int getSelectedMainTabIndex() {
		int index =  tabPane.getSelectedIndex();
		if (index == tabPane.getTabCount() - 1) {
			index = TAB_MAIN_ABOUT;
		}
		return index;
	}

	public int getSelectedOwnerTabIndex() {
		return ownerTabbedPane.getSelectedIndex();
	}

	public int getSelectedUserTabIndex(int index) {
		SuittarTabbedPane userTabbedPane = (SuittarTabbedPane)tabPane.getComponentAt(index);
		return userTabbedPane.getSelectedIndex();
	}

	public int addNewUserTab(UserWithStatus user,Suittar suittar) {
		SuittarTabbedPane userPane = new SuittarTabbedPane(user);
		for (SuittarMainPane pane:userPane.getPaneAll()) {
			pane.setMainPaneFont(font);
		}
		userPane.setSelectedIndex(SuittarTabbedPane.TAB_OTHER_PROFILE);
		userPane.addChangeListener(suittarTabChangeListener);
		for (SuittarMainPane pane:userPane.getPaneAll()) {
			pane.addSuittarHyperLinkListener(suittar);
		}
		tabPane.insertTab(user.getScreenName(), null, userPane, "", tabPane.getTabCount() - 1);
		userTabArray.add(user.getId());
		//getTabCount() - 1 - 1(Aboutパネルの分 -1 する)
		return tabPane.getTabCount() - 2;
	}
	
	public int findUserTab(int id) {
		if (!userTabArray.contains(id)) {
			return -1;
		}
		//+2 は Public と Home のTAB数を加算する
		return userTabArray.indexOf(id) + 2;
	}
	
	public SuittarMainPane getMainPane(int index,int subIndex) {
		if (index == TAB_MAIN_PUBLIC) {
			return publicMainPane;
		}
		else if (index == TAB_MAIN_MYUSER) {
			return ownerTabbedPane.getPane(subIndex);
		}
		else if (index == TAB_MAIN_ABOUT){
			return aboutMainPane;
		}
		else {
			SuittarTabbedPane userTabbedPane = (SuittarTabbedPane) tabPane.getComponentAt(index);
			return userTabbedPane.getPane(subIndex);
		}
	}
	
	public void removeUserTab(int id) {
		int index = findUserTab(id);
		tabPane.remove(index);
		//-2 は Public と Home のTAB数を加算する
		userTabArray.remove(index -  2);
		tabPane.setSelectedIndex(TAB_MAIN_MYUSER);
	}
	
	public UserWithStatus getUserByTagIndex(int index) {
		SuittarTabbedPane userTabbedPane = (SuittarTabbedPane) tabPane.getComponentAt(index);
		return userTabbedPane.getUser();		
	}
	
	public void addAllEventListener(Suittar suittar) {
		inputPane.addButtonActionListener(suittar);
		statusBar.addSuitarStatusBarActionListener(suittar);
		publicMainPane.addSuittarHyperLinkListener(suittar);
		for (SuittarMainPane pane:ownerTabbedPane.getPaneAll()) {
			pane.addSuittarHyperLinkListener(suittar);
		}
		tabPane.addChangeListener(new SuittarTabChangeListener(suittar,true));
		ownerTabbedPane.addChangeListener(suittarTabChangeListener);
	}
	
}
